package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.ApAuthorService;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.BeanHelper;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.AuthorDto;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2022-04-29
 */
@Service
@Slf4j
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements ApAuthorService {

    /**
     * 保存作者
     * @param dto
     * @return
     */
    @Override
    public Integer saveAuthor(AuthorDto dto) {
//        查询作者
        LambdaQueryWrapper<ApAuthor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApAuthor::getUserId,dto.getUserId());
        ApAuthor apAuthor = getOne(queryWrapper);
//        没有查询到 新增
        if(apAuthor == null){
            apAuthor = new ApAuthor();
            apAuthor.setUserId(dto.getUserId());
            apAuthor.setWmUserId(dto.getWmUserId());
        }
        apAuthor.setType(dto.getType());
        apAuthor.setName(dto.getName());
//        修改
        boolean b = this.saveOrUpdate(apAuthor);
        if(!b){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return apAuthor.getId();
    }

    /**
     * 根据id查询
     *
     * @param wmUserId
     * @return
     */
    public ApAuthor getByWmUserId(Integer wmUserId) {

        LambdaQueryWrapper<ApAuthor> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ApAuthor::getUserId,wmUserId);

        ApAuthor apAuthor = getOne(queryWrapper);

        return apAuthor;
    }



}
